<script setup>
import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
import { onLoad, onShow } from '@dcloudio/uni-app'

import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
import { ref } from 'vue'
import dayjs from 'dayjs'
import { addFinancialApplyControllerApi } from '@/api/attendance'
import { getUserProfile } from '@/api/system/user'
import approvalProcessState from '@/store/modules/approvalProcess'
import { isObjectValuesNotEmpty } from '@/utils/objIsnull.js'
import { listTreeApi } from '@/api/dep'

const title = '非经营性付款申请'
const expensesRef = ref(null)
const assumptionCostsDepRef = ref(null)
const paymentMethodsRef = ref(null)
const paymentDateRef = ref(null)
const actualDateRef = ref(null)
const OperatingPaymentsList = ref({
  assumptionCostsDep: '',
  assumptionCostsDepName: '',
  reasonPayment: '',
  expenses: '',
  paymentMethods: '',
  paymentDate: '',
  payee: '',
  bankAccount: '',
  bankName: '',
  actualDate: '',
  remark: '',
  costDeptId:0
})
const open = ref(false)
const selectList = ref([

])
const assumptionCostsDepFn = () => {
  uni.navigateTo({
    url: '/components/OptionDeptPerson/deptName?type=dept',
  })
}
const assumptionCostsDepColumns = ref([
  [

  ],
])
const assumptionCostsDepConfirm = (e) => {
  OperatingPaymentsList.value.assumptionCostsDep = e.value[0]
}
let attchment = ref([])
const userId = ref(null)
const getDesignDrawingsEmit = (emit) => {
  attchment.value = emit
}
const clickSave = async () => {
  const RequiredItemList = JSON.parse(JSON.stringify(OperatingPaymentsList.value))
  delete RequiredItemList.actualDate
  delete RequiredItemList.remark
  if (isObjectValuesNotEmpty(RequiredItemList)) {
    let params = {
      deptId: OperatingPaymentsList.value.assumptionCostsDep,
      reason: OperatingPaymentsList.value.reasonPayment,
      costCategory: OperatingPaymentsList.value.expenses,
      money: OperatingPaymentsList.value.paymentMoney,
      paymentMethod: OperatingPaymentsList.value.paymentMethods,
      date: dayjs(OperatingPaymentsList.value.paymentDate).format('YYYY-MM-DD hh:mm:ss'),
      payeeFullName: OperatingPaymentsList.value.payee,
      bankAccount: OperatingPaymentsList.value.bankAccount,
      bankOfDeposit: OperatingPaymentsList.value.bankName,
      costDeptId: OperatingPaymentsList.value.costDeptId,
      date2: OperatingPaymentsList.value.actualDate
        ? dayjs(OperatingPaymentsList.value.actualDate).format('YYYY-MM-DD hh:mm:ss')
        : '',
      remark: OperatingPaymentsList.value.remark,
      ruleId: 'feijingying',
      type: '非经营性付款申请',
      attchment: JSON.stringify(attchment.value),
      userId: userId.value,
      approve: approvalProcessState.state.approvalProcess,
    }
    let { code } = await addFinancialApplyControllerApi(params)
    if (code === 200) {
      uni.showToast({
        title: '提交成功',
        duration: 1500,
        icon: 'success',
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1000)
    }
  } else {
    uni.showToast({
      title: '请输入完整数据',
      icon: 'error',
    })
  }
}
const expensesColumns = [['维系费', '业务提成', '暂收款', '员工分红', '其他']]
const expensesConfirm = (e) => {
  OperatingPaymentsList.value.expenses = e.value[0]
}
const expensesFn = () => {
  expensesRef.value.open()
}
const formatter = (type, value) => {
  if (type === 'year') {
    return `${value}年`
  }
  if (type === 'month') {
    return `${value}月`
  }
  if (type === 'day') {
    return `${value}日`
  }
  if (type === 'hour') {
    return `${value}时`
  }
  if (type === 'minute') {
    return `${value}分`
  }
  return value
}

const paymentMethodsColumns = [['银行卡', '现金', '汇票', '电汇', '贷记', '支票', '其他']]
const paymentMethodsConfirm = (e) => {
  OperatingPaymentsList.value.paymentMethods = e.value[0]
}
const paymentMethodsFn = () => {
  paymentMethodsRef.value.open()
}

const paymentDateFn = () => {
  paymentDateRef.value.open()
}
let paymentDatePicker = dayjs()
const paymentDateConfirm = (obj) => {
  OperatingPaymentsList.value.paymentDate = dayjs(obj.value).format('YYYY-MM-DD')
}

const actualDateFn = () => {
  if (OperatingPaymentsList.value.paymentDate) {
    actualDateRef.value.open()
  } else {
    uni.showToast({
      title: '请先选择付款日期',
      icon: 'none',
    })
  }
}
let actualDatePicker = dayjs()
const actualDateConfirm = (obj) => {
  OperatingPaymentsList.value.actualDate = dayjs(obj.value).format('YYYY-MM-DD')
}

const getUserProfileFn = async () => {
  let { code, data } = await getUserProfile()
  if (code === 200) {
    userId.value = data.userId
  }
}

const listTreeApiFn = async () => {
  let { code, data } = await listTreeApi()
  if (code === 200) {
    selectList.value = data
  }
}

onLoad(async () => {
  await getUserProfileFn()
  await listTreeApiFn()
})
onShow(() => {
  // if(optionDeptPerson.state.optionDept1){
  //   OperatingPaymentsList.value.assumptionCostsDepName = optionDeptPerson.state.optionDept1.label
  //   OperatingPaymentsList.value.assumptionCostsDep = optionDeptPerson.state.optionDept1.id
  // }
  // 监听事件 获取选择的部门
  uni.$on('getOptionDept', (data) => {
    // console.log(data, '部门data')
    OperatingPaymentsList.value.assumptionCostsDepName = data.label
    OperatingPaymentsList.value.assumptionCostsDep = data.id ? data.id : data.depId // id 子部门id depId部门id
  })
})
const goHistoryView = () => {
  uni.navigateTo({
    url: '/components/TypeDefineModel?type=非经营性付款申请&apiName=财务',
  })
}

const changeSelect = (item, index) => {
  OperatingPaymentsList.value.assumptionCostsDep  = item.label
  OperatingPaymentsList.value.costDeptId  = item.id

}
</script>

<template>
  <view>
    <!--    非经营性付款申请  -->
    <HeaderNavTabarSvg :title="title">
      <image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
    </HeaderNavTabarSvg>

    <view class="picker-content" @tap="open = true">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">费用承担部门</text>
        <view class="info-list-right">
          <view v-if="OperatingPaymentsList.assumptionCostsDep" class="uni-input"
          >{{ OperatingPaymentsList.assumptionCostsDep }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>


    <liy-select :list="selectList"
                :open="open"
                @close="open = false"
                @change="changeSelect"
                titleKey="label"
                subtitleKey="address"
    ></liy-select>
    <view class="textarea-box">
      <text class="info-list-left isrequired">付款事由</text>
      <textarea
        v-model="OperatingPaymentsList.reasonPayment"
        class="textinfo-list"
        placeholder="请输入"
      ></textarea>
    </view>

    <view class="picker-content" @tap="expensesFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">费用类别</text>
        <view class="info-list-right">
          <view v-if="OperatingPaymentsList.expenses" class="uni-input"
            >{{ OperatingPaymentsList.expenses }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>

    <uv-picker ref="expensesRef" :columns="expensesColumns" @confirm="expensesConfirm"></uv-picker>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">付款金额</text>
        <view class="info-list-right">
          <view class="info-content" style="display: flex; align-items: center">
            <input
              v-model="OperatingPaymentsList.paymentMoney"
              adjust-position
              class="uni-input"
              placeholder="请输入"
              type="text"
            />
            <p style="font-size: 28rpx; line-height: 1.4em">元</p>
          </view>
        </view>
      </view>
    </view>
    <view class="picker-content" @tap="paymentMethodsFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">付款方式</text>
        <view class="info-list-right">
          <view v-if="OperatingPaymentsList.paymentMethods" class="uni-input"
            >{{ OperatingPaymentsList.paymentMethods }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-picker
      ref="paymentMethodsRef"
      :columns="paymentMethodsColumns"
      @confirm="paymentMethodsConfirm"
    ></uv-picker>
    <view class="picker-content" @tap="paymentDateFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">付款日期</text>
        <view class="info-list-right">
          <view v-if="OperatingPaymentsList.paymentDate" class="uni-input"
            >{{ OperatingPaymentsList.paymentDate }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="paymentDateRef"
      v-model="paymentDatePicker"
      :formatter="formatter"
      mode="date"
      @confirm="paymentDateConfirm"
    >
    </uv-datetime-picker>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">收款人全称</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              v-model="OperatingPaymentsList.payee"
              adjust-position
              class="uni-input"
              placeholder="请输入"
              type="text"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">银行账号</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              v-model="OperatingPaymentsList.bankAccount"
              adjust-position
              class="uni-input"
              placeholder="请输入"
              type="text"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">开户行</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              v-model="OperatingPaymentsList.bankName"
              adjust-position
              class="uni-input"
              placeholder="请输入"
              type="text"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content" @tap="actualDateFn">
      <view class="measurement-info-list">
        <text class="info-list-left">实际支付日期</text>
        <view class="info-list-right">
          <view v-if="OperatingPaymentsList.actualDate" class="uni-input"
            >{{ OperatingPaymentsList.actualDate }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="actualDateRef"
      v-model="actualDatePicker"
      :formatter="formatter"
      :min-date="OperatingPaymentsList.paymentDate"
      mode="date"
      @confirm="actualDateConfirm"
    >
    </uv-datetime-picker>

    <view class="textarea-box">
      <text class="info-list-left">备注说明</text>
      <textarea
        v-model="OperatingPaymentsList.remark"
        class="textinfo-list"
        placeholder="请输入"
      ></textarea>
    </view>

    <!--    说明附件-->
    <DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit" />
    <!--    审批流程 :money="OperatingPaymentsList.paymentMoney" 不需要-->
    <AuditApplicationSteps :ruleId="'feijingying'" :costDeptId = OperatingPaymentsList.costDeptId />
    <!--    底部占位符-->
    <view class="blank"></view>
    <!--    保存-->
    <view class="buttonContainer">
      <button class="buttonClass" @click="clickSave">提交</button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import '@/static/style/index.scss';

.bukaSvg {
  width: 32rpx;
  height: 32rpx;
}

.buttonContainer {
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: center;
  width: 100%;
  align-items: center;
  padding: 16rpx;
  background-color: #fff;
}

.buttonClass {
  display: flex;
  width: 686rpx;
  height: 72rpx;
  justify-content: center;
  align-items: center;
  background: #165dff;
  border-radius: 16rpx;
  color: #fff;
  font-size: 32rpx;
  //margin-top: 40rpx;
  overflow: hidden;
}

.uni-inputs {
  color: #86909c !important;
}

.uni-input {
  text-align: right;
  font-size: 28rpx;
}

.blank {
  height: calc(72rpx + 32rpx + 20rpx);
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.bukabanci {
  .info-content {
    color: #86909c !important;
  }

  .info-list-left {
    // @include font-color-auxiliary;
    color: #86909c !important;
  }
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.popup-content {
  background: #f7f8fa;

  .header-box {
    padding: 30rpx 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    p {
      color: #1d2129;
      font-size: 34rpx;
      font-weight: 500;
    }

    .module-image-ba {
      border-radius: 32rpx;
      background: #f2f3f5;
      width: 45rpx;
      height: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }

  .card-list {
    display: flex;
    align-items: center;
    flex-direction: column;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .list {
      box-shadow: 0 4rpx 16rpx 0 #0000000d;
      box-sizing: border-box;
      width: 100%;
      background: #fff;
      padding: 16rpx 24rpx;
      border-radius: 8rpx;
      margin-bottom: 10rpx;

      h4 {
        color: #1d2129;
        font-size: 32rpx;
        font-weight: 400;
      }

      p {
        color: #86909c;
        font-size: 24rpx;
        font-weight: 400;
        margin-top: 10rpx;
      }
    }
  }
}

.measurement-info-list {
  @include flex($align: center, $justify: space-between);
  padding: $spacing-row-base;
  background: #fff;
  margin-top: 16rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .info-list-right {
    @include flex($align: center);

    .info-content {
      @include textStyleBase;
      font-size: 34rpx;
    }

    .iconfont {
      margin-left: 16rpx;
      // @include font-color-auxiliary;
      color: $uni-fc-fu-lin;
    }
  }

  .unit {
    // @include font-color-auxiliary;
    color: $uni-fc-fu-lin;
  }
}
</style>